的用戶輸入值列因此,例如,我的數據集是產品與閃亮
col1<-rep(c("a","b","c","d"),3)
col2<-rep(c(1:4),3)
col3<-rep(c("l","m","n"),4)
col4<-rep(c(9:12),3)
df<-data.table(col1,col2,col3,col4)
我想創建一個新的DataTable,結果就是從給定的與值每列的產品用戶然後將它們總結爲一個像
final=l*value1+m*value2
問題是如何使用該值創建每列的產品值。
荷拉是我的應用程序
library(shiny)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Old Faithful Geyser Data"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
textInput("value",
"value"),
selectInput("choice",
"choice",unique(df$col3)),
actionButton("update", "Update"),actionButton("calculation", "calculation")
),
# Show a plot of the generated distribution
mainPanel(
dataTableOutput("table"),dataTableOutput("table2"),dataTableOutput("combined")
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
Data<- reactive({
if(input$choice == "NULL") {data <- df}
else {data <- subset(df, col1 == input$choice)}
data
})
output$table<- renderDataTable(Data())
dat<-c()
table2 <- eventReactive(input$update, {
if(!is.null(input$value) && input$update>0){
newrow = data.table(value= input$value,choice=input$choice)
dat <<- rbind(dat, newrow)
}
dat
}, ignoreNULL = FALSE)
output$table2<- renderDataTable(table2())
data_wide <-dcast(df, value.var= "col2", col1~col3)
new<-c()
alldata<-eventReactive(input$calculation, {
for (i in table2()$choice)
{ new<-c(new,i)}
data_new<-na.omit(subset(data_wide, select=c("col1",new)))
})
output$combined <- DT::renderDataTable(alldata())
}
# Run the application
shinyApp(ui = ui, server = server)
表2的結果將是
value choice
2 l
3 m
,並且如果用戶選擇列升和值2和m列和值3的結果輸出時便會最後一列
value1 l value2 m final
2 2 3 3 5
2 4 6 3 10
2 6 9 3 15
2 8 12 3 20
我在下面添加了這個行但是發生了這個錯誤 Warning :*中的錯誤:二進制運算符的非數字參數
product<-eventReactive(input$calculation, {
for(j in 2:ncol(alldata())){
set(alldata, i = NULL, j = j, value = alldata()[[j]] * table2()$value[j-1])
}
alldata()
})
output$product <- DT::renderDataTable(product())
}
# Run the application
shinyApp(ui = ui, server = server)
我不從你的描述瞭解你們的計算的代碼是什麼不是很清楚。你能更具體地瞭解某些輸入的期望輸出嗎? – MrFlick
但是'm'定義在哪裏? 「2 * m」「去」的確切位置在哪裏? – MrFlick
@MrFlick我試圖讓它更清楚我希望它有幫助 – Aleha