我正在创建我的第一个闪亮的应用程序,当使用ggplot2但是使用其他基础R或vcd图时,一切都很棒。我希望用户能够选择一个表格变量,然后查看生成的马赛克或关联图。我的服务器代码在table命令失败。我已经尝试过的事情在下面说明了。
感谢您的帮助。
library(shiny)
library(shinydashboard)
library(vcd)
header = dashboardHeader(title = 'Min Reproducible Example')
sidebar = dashboardSidebar()
body = dashboardBody(
fluidRow(plotOutput('plot'), width=12),
fluidRow(box(selectInput('factor', 'Select Factor:', c('OS', 'Gender'))))
)
ui = dashboardPage(header, sidebar, body)
server = function(input, output){
set.seed(1)
df = data.frame(Condition = rep(c('A','B','C','D'), each = 300),
Conversion = c(sample(c('Convert','Not-Convert'), 300, replace = TRUE, prob = c(0.9, 0.1)),
sample(c('Convert','Not-Convert'), 300, replace = TRUE, prob = c(0.7, 0.3)),
sample(c('Convert','Not-Convert'), 300, replace = TRUE, prob = c(0.5, 0.5)),
sample(c('Convert','Not-Convert'), 300, replace = TRUE, prob = c(0.2, 0.8))),
Gender = sample(c('M','F'), 1200, replace = TRUE),
OS = rep(sample(c('Web','iOS','Android'), 1200, replace = TRUE), times = 2))
#tried this
#table1 = reactive({
# with(df, table(Condition, Conversion, input$factor))
#})
output$plot = renderPlot({
#fails here:
table1 = with(df, table(Condition, Conversion, input$factor))
#also tried these
#table1 = with(df, table(Condition, Conversion, as.character(isolate(reactiveValuesToList(input$factor)))))
#also tried table1 = with(df, table(Condition, Conversion, input$factor))
#also tried table1 = table(df$Condition, df$Conversion, paste0('df$', input$factor))
#then I want some categorical plots
assoc(table1, shade=TRUE)
#or mosaicplot(table1, shade=TRUE)
})
}
shinyApp(ui, server)
答案 0 :(得分:0)
一个简单的解决方法是使用' starts_with'来自输入变量
的select()语句中的dplyrlibrary('dplyr')
output$plot = renderPlot({
df <- select(df, Condition, Conversion, tmp_var = starts_with(input$factor))
table1 = with(df, table(Condition, Conversion, tmp_var))
mosaicplot(table1, shade=TRUE)
})
}