【发布时间】:2022-01-16 20:56:29
【问题描述】:
我有一个表,用户将在其中输入一些组。因此,我希望另一列自动更新并显示每个组的频率(或重复):
这段代码创建了这个应用程序:
library(shiny)
library(rhandsontable)
library(tidyverse)
# Define UI for application that draws a histogram
ui <- fluidPage(
# Application title
titlePanel("Automatic data rhandsontable"),
# Sidebar with a slider input for number of bins
sidebarLayout(
sidebarPanel(
),
# Show a plot of the generated distribution
mainPanel(
rhandsontable::rHandsontableOutput('ed_out'),
shiny::actionButton('start_input', 'save final table')
)
)
)
# Define server logic required to draw a histogram
server <- function(input, output) {
# This has to be reactive
data <- reactive({
df <- data.frame(Animal = c('Dog', 'Cat', 'Mouse', 'Elephant', 'Tiger'),
Group = ' ',
replicate = as.numeric(' '))
})
output$ed_out <- rhandsontable::renderRHandsontable({
df <- data()
rhandsontable(
df,
height = 500,
width = 600) %>%
hot_col('replicate', format = '0a', readOnly = TRUE) %>%
hot_col('Animal', readOnly = TRUE)
})
# This is just to save the table when the user has finished, can be ignored
group_finals <- reactiveValues()
observeEvent(input$start_input, {
group_finals$data <- rhandsontable::hot_to_r(input$ed_out)
print(group_finals$data)
}
)
}
# Run the application
shinyApp(ui = ui, server = server)
所以这个想法是用户输入组和复制自动更新:(这里用户输入B, B, A, A, B。
我能够计算每个组的重复数,但我不确定如何实现这部分来计算它们并在用户输入每个组后同时显示它们。
df <- df %>%
group_by(Group) %>%
mutate(replicate = 1:n())
不确定这是否是最好的方法,我尝试使用hot_to_col renderer 来使用javascript,但我不熟悉那种语言。
【问题讨论】:
标签: r shiny rhandsontable