【发布时间】:2019-10-09 21:10:23
【问题描述】:
我希望我的闪亮应用能够绘制具有不同列数的数据框。由于我不希望数据包含超过 5 列,因此我为 ggplot 对象编写了所有可能情况的明确列表。我还希望使用 UI 上的滑块来改变第一个对象的大小。
问题在于该滑块的值仅适用于多列数据。
为了测试这一点,这里有一个数据列的示例数据: https://drive.google.com/file/d/182Qi-2I37OscSeLir_AyXdoedstIxx6D/view?usp=sharing
这里有两个: https://drive.google.com/file/d/1eB0eKvgfj94xIp80P0SG3QnoGZJ1Zq9a/view?usp=sharing
这是我在这里的第一个问题,我不确定如何理想地为使用 fileInput 的闪亮应用程序提供示例。如果有比这更好的方法,请告诉我。
#The ui
shinyUI(
pageWithSidebar(
headerPanel(""),
sidebarPanel(
fileInput(inputId = "file", label = "Choose an Excel (.xlsx) File",
multiple = FALSE,
accept = c("text/xlsx", "text/microsoft-excel-pen-XML-format-spreadsheet-file", ".xlsx"),
width = NULL, buttonLabel = "Find...",
placeholder = "No File selected"
),
sliderInput(inputId = "width", label = "width", min = 0.01, max = 0.4, value = 0.2, step = 0.01)),
mainPanel(plotOutput("graph"), height = "600px", quoted = TRUE)
)
)
#and the server
library(shiny)
library(ggplot2)
library(readxl)
shinyServer(function(input, output, session) {
data <- reactive({
data.frame(read_xlsx(input$file$datapath))
})
output$graph <- renderPlot({
print(
#somehow the interactive heigth setting is now muted within the expression
ggplot(data(), aes(x = data()[,1], y = "", fill = as.factor(data()[2:ncol(data())])) ) +
{if(ncol(data()) >= 2)geom_tile(aes(x = data()[,1], y = 0, fill = as.factor(data()[,2])), data(), width = 0.2, height = input$width, size = 2)} +
{if(ncol(data()) >= 3)geom_tile(aes(x = data()[,1], y = 0.125, fill = as.factor(data()[,3])), data(), width = 0.2, height = 0.02, size = 2)} +
{if(ncol(data()) >= 4)geom_tile(aes(x = data()[,1], y = -0.125, fill = as.factor(data()[,4])), data(), width = 0.2, height = 0.02, size = 2)} +
{if(ncol(data()) == 5)geom_tile(aes(x = data()[,1], y = -0.145, fill = as.factor(data()[,5])), data(), width = 0.2, height = 0.02, size = 2)}
scale_y_continuous(breaks = NULL, labels = NULL)
)
})
})
【问题讨论】: